# from selenium import webdriver
import time

from selenium import webdriver
from appium import webdriver as app_driver
from selenium.webdriver.common.by import By


class UtilsDriver:
    _front_desk_driver = None
    _background_driver = None
    _app_driver = None

    @classmethod
    def get_front_driver(cls):
        if cls._front_desk_driver is None:
            cls._front_desk_driver = webdriver.Chrome()
            cls._front_desk_driver.maximize_window()
            cls._front_desk_driver.implicitly_wait(10)
            cls._front_desk_driver.get("http://hahaha.tpshop.com/")
        return cls._front_desk_driver

    @classmethod
    def quit_driver(cls):
        if cls._front_desk_driver is not None:
            cls.get_front_driver().quit()
            cls._front_desk_driver = None

    @classmethod
    def get_background_driver(cls):
        if cls._background_driver is None:
            cls._background_driver = webdriver.Chrome()
            cls._background_driver.maximize_window()
            cls._background_driver.implicitly_wait(10)
            cls._background_driver.get("http://localhost/admin")
        return cls._background_driver

    @classmethod
    def quit_back_driver(cls):
        if cls._background_driver is not None:
            cls.get_background_driver().quit()
            cls._background_driver = None

    @classmethod
    def get_app_driver(cls):
        if cls._app_driver is None:
            des_cap = {
                "platformName": "android",
                "platformVersion": "7.1.2",
                "deviceName": "****",
                "appPackage": "com.netease.newsreader.activity",
                "appActivity": "com.netease.nr.phone.main.MainActivity",
                "noReset": True
            }
            cls._app_driver = app_driver.Remote("http://localhost:4723/wd/hub", des_cap)
        return cls._app_driver

    @classmethod
    def quit_app_quit(cls):
        if cls._app_driver is not None:
            cls.get_app_driver().quit()
            cls._app_driver = None


def element_is_exist(driver, text):
    xpath = "//*[contains(text(),'{}']".format(text)
    try:
        time.sleep(3)
        return driver.find_element(By.XPATH, xpath)
    except Exception as e:
        return False


def app_swipe_find(driver, element, element_info):
    # 定位元素的大小
    element_size = element.size
    width = element_size["width"]
    height = element_size["height"]
    # 定位元素的位置
    element_location = element.location
    x = element_location["x"]
    y = element_location["y"]

    start_x = x + width * 0.9
    start_y = y + height * 0.5
    end_x = x + width * 0.1
    end_y = y + height * 0.5

    while True:
        page_source = driver.page_source
        try:
            driver.find_element(*element_info).click()
            return True
        except Exception as e:
            print("没有找到该元素")
            driver.swipe(start_x, start_y, end_x, end_y, duration=1000)
            time.sleep(3)
        if page_source == driver.page_source:
            print("滑屏完成没有在该页面找到元素")
            return False
